<?php

class Reality extends Item {
	
	protected $city = NULL;
	
	protected $agent = NULL;

	protected $propertyList = array();

	protected $imageList = array();
	
	public function getUrl() {
		$pageRepository = new PageRepository();
		$page = $pageRepository->getOneByUid($this['pid']);
		return $page['alias'] . '/' . $this['alias'] . '.html';
	}
	
	public function getCity() {
		if ($this->city === NULL) {
			$cityRepository = new CityRepository();
			$this->city = $cityRepository->getOneByUid($this['fk_city_id']);
		}
		return $this->city;
	}
	
	public function getAgent() {
		if ($this->agent === NULL) {
			$agentRepository = new AgentRepository();
			$this->agent = $agentRepository->getOneByUid($this['fk_agent_id']);
		}
		return $this->agent;
	}
	
	public function getPropertyList() {
		if (empty($this->propertyList)) {
			$query = 'SELECT uid_foreign FROM reality_property WHERE uid_local = ' . $this['uid'];
			$realityProperties = DB::fetchAll($query);
			$propertyUidList = array();
			foreach ($realityProperties as $realityProperty) {
				$propertyUidList[] = $realityProperty['uid_foreign'];
			}
			$propertyRepository = new PropertyRepository();
			$this->propertyList = $propertyRepository->getByUid($propertyUidList);
		}
		return $this->propertyList;
	}
	
	public function getLeftProperties() {
		$properties = $this->getPropertyList();
		$leftProperties = array();
		for ($i = 0; $i < count($properties); $i++) {
			if ($i % 2 == 0) {
				$leftProperties[] = $properties[$i];
			}
		}
		return $leftProperties;
	}
	
	public function getRightProperties() {
		$properties = $this->getPropertyList();
		$rightProperties = array();
		for ($i = 0; $i < count($properties); $i++) {
			if ($i % 2 == 1) {
				$rightProperties[] = $properties[$i];
			}
		}
		return $rightProperties;
	}
	
	public function getMainImage() {
		if ($this['main_image']) {
			return 'uploads/tx_reality/reality/' . $this['main_image'];
		}
		return '';
	}
	
	public function getImageList() {
		if (empty($this->imageList)) {
			$query = 'SELECT uid_foreign FROM reality_image WHERE uid_local = ' . $this['uid'];
			$realityImages = DB::fetchAll($query);
			$imageUidList = array();
			foreach ($realityImages as $realityImage) {
				$imageUidList[] = $realityImage['uid_foreign'];
			}
			$imageRepository = new ImageRepository();
			$this->imageList = $imageRepository->getByUid($imageUidList);
		}
		return $this->imageList;
	}
}

?>